/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.dao.ViewHistoryDao;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.entity.ViewHistory;
import com.hboxs.asl.service.ViewHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Service - 商品浏览记录
 */
@Service("viewHistoryServiceImpl")
public class ViewHistoryServiceImpl extends BaseServiceImpl<ViewHistory, Long> implements ViewHistoryService {

    @Resource(name = "viewHistoryDaoImpl")
    private ViewHistoryDao viewHistoryDao;

    @Resource(name = "viewHistoryDaoImpl")
    public void setBaseDao(ViewHistoryDao viewHistoryDao) {
        super.setBaseDao(viewHistoryDao);
    }

    @Override
    public boolean historyExists(Member member, Product product) {
        return viewHistoryDao.historyExists(member, product);
    }

    @Override
    public ViewHistory getViewHistory(Member member, Product product) {
        return viewHistoryDao.getViewHistory(member, product);
    }

    @Override
    public List<ViewHistory> getMyViewHistory(Member member) {
        return viewHistoryDao.getMyViewHistory(member);
    }

    @Override
    public void create(Member member, Product product) {
        if (member != null) {
            if (historyExists(member, product)) {
                ViewHistory viewHistory = getViewHistory(member, product);
                viewHistory.setDate(new Date());
                update(viewHistory);
            } else {
                save(new ViewHistory(member, product, new Date()));
            }
        }
    }
}
